define(['zepto','utils', 'sui'], function($, utils) {
  return {
    init: function(data) {
      this.data = data;
      this.initSearchMovement();
      this.initAddMovement();
      this.initAdd();
    },
    
    initSearchMovement: function() {
      var that = this;
      
      $(document).on("click", ".search-button", function() {
        $.showIndicator()
        $.get(window.__CTX__.root + '/movements?filter=true&name=' + $("#search").val())
          .done(function(response) {
            $.hideIndicator()
            utils.processResult(response, function(result) {
              that.showList(result.data);
            }, function(result) {
              $.toast(result.message || '查询失败')
            })
          })
      })
    },
    
    showList: function(response) {
      var that = this;
      $(".movement-box").empty();
      var html = "<div class='search-result'>";
      
      if(response.movements) {
        html = html + "<div class='list-block media-list'><form name='add_form'><ul>"
        for(var i in response.movements) {
          html = html + '<li class="movement-radio">'
                      + '<label class="label-checkbox item-content">'
                      + '<input type="checkbox" name="movementId" value='+response.movements[i].id+'>'
                      + '<div class="item-media"><i class="icon icon-form-checkbox"></i></div>'
                      + '<div class="item-inner"><div class="item-title-row">'
                      + '<div class="item-title">'+response.movements[i].name+'</div>'
                      + '</div>'
                      + '<div class="item-subtitle">'+ response.movements[i].summary + '</div>'
                      + '</div>'
                      + '</label>'
                      + '</li>';
        }
        
        html = html + '</ul></form></div>';
      } else {
        html = html + "<p class='text-center' style='color: #999;'></p>"
      }
      
      html = html + '</div>'
      $(".movement-box").append(html);
    },
    
    showModal: function(value) {
      var that = this;
      var html = '<input name ="groupAmount" value="' + that.data.groupAmount + '" placeholder="请输入组数" style="display: block; width: 100%; height: 1.6rem; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;" />' +
                 '<input name ="movementNumber" value="' + that.data.movementNumber + '" placeholder="请输入每组动作次数" style="display: block; width: 100%; height: 1.6rem; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;" />' + 
                 '<input name ="maxWeight" value="' + that.data.maxWeight + '" placeholder="请输入最大重量" style="display: block; width: 100%; height: 1.6rem; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;" />' + 
                 '<input name ="time" value="' + that.data.time + '" placeholder="请输入计时" style="display: block; width: 100%; height: 1.6rem; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;" />' +
                 '<textarea name="remark" placeholder="请输入备注" rows=3 style="display: block; width: 100%; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;">'+that.data.remark+'</textarea>';
      var currentModal = $.modal({
        title:  '添加参数',
        text: html,
        buttons: [
          {
            text: '取消'
          },{
            text: '确定',
            close: false,
            onClick: function() {
              var groupAmount = $("input[name='groupAmount']").val();
              var movementNumber = $("input[name='movementNumber']").val();
              var maxWeight = $('input[name="maxWeight"]').val();
              var time = $('input[name="time"]').val();
              var remark = $('textarea[name="remark"]').val();
              
              if(!groupAmount) {
                $("input[name='groupAmount']").val(0);
              } else if(isNaN(groupAmount)) {
                  $.toast("组数请输入数字")
                  return false;
              }
              
              if(!movementNumber) {
                $("input[name='movementNumber']").val(0);
              } else if(isNaN(movementNumber)) {
                $.toast("每组次数请输入数字")
                return false;
              }
              
              that.submit(value, groupAmount, movementNumber, maxWeight, time, remark);
              $.closeModal(currentModal)
            }
          }
        ]
      })
    },
    
    submit: function(value, groupAmount, movementNumber, maxWeight, time, remark) {
      var that = this;
      
      var url;
      var para;
      if(that.data.coursewareId) {
        url = window.__CTX__.root+'/rounds/'+that.data.roundId+'/course-coursewares/'+that.data.courseScheduleId+'/coursewares/' + that.data.coursewareId;
        para = {"_method": "PATCH","coursewareId": that.data.coursewareId,"movement": value, "groupAmount": !groupAmount ? 0 : groupAmount, "movementNumber": !movementNumber ? 0 : movementNumber, "maxWeight": maxWeight, "time": time, "remark": remark};
      } else {
        url = window.__CTX__.root+'/rounds/'+that.data.roundId+'/course-coursewares/'+that.data.courseScheduleId+'/coursewares/';
        para = {"movement": value, "groupAmount": !groupAmount ? 0 : groupAmount, "movementNumber": !movementNumber ? 0 : movementNumber, "maxWeight": maxWeight, "time": time, "remark": remark};
      }
      $.showIndicator();
      $.post(url, para)
      .done(function(response) {
        $.hideIndicator()
        utils.processResult(response, function(result) {
          window.location.href = window.__CTX__.root + '/course-schedules?day='+that.data.day+'&hour='+that.data.hour
        }, function(result) {
          $.toast(result.message || '提交失败')
        })
      })
    },
    
    initAddMovement: function() {
      var that = this;
      
      $(document).on("click", ".add-button", function() {
        var html = '<input name ="name" placeholder="请输入动作名称" style="display: block; width: 100%; height: 1.6rem; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;" />'/*+
                   '<input name ="groupAmount" value="' + that.data.groupAmount + '" placeholder="请输入组数" style="display: block; width: 100%; height: 1.6rem; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;" />' +
                   '<input name ="movementNumber" value="' + that.data.movementNumber + '" placeholder="请输入每组动作次数" style="display: block; width: 100%; height: 1.6rem; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;" />' + 
                   '<input name ="maxWeight" value="' + that.data.maxWeight + '" placeholder="请输入最大重量" style="display: block; width: 100%; height: 1.6rem; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;" />' + 
                   '<input name ="time" value="' + that.data.time + '" placeholder="请输入计时" style="display: block; width: 100%; height: 1.6rem; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;" />' +
                   '<textarea name="remark" placeholder="请输入备注" rows=3 style="display: block; width: 100%; margin: 0 0 .3rem 0; padding: 0 .4rem; font-size: .7rem;">'+that.data.remark*/+'</textarea>';
        var currentModal = $.modal({
         title:  '添加动作',
         text: html,
         buttons: [
           {
             text: '取消'
           },{
             text: '确定',
             close: false,
             onClick: function() {
               var name = $("input[name='name']").val();
               /*var groupAmount = $("input[name='groupAmount']").val();
               var movementNumber = $("input[name='movementNumber']").val();
               var maxWeight = $('input[name="maxWeight"]').val();
               var time = $('input[name="time"]').val();
               var remark = $('textarea[name="remark"]').val();*/
               
               if(!name || $.trim(name) == '') {
                 $.toast("请输入动作名称");
                 return false;
               }
               
               /*if(!groupAmount) {
                 $("input[name='groupAmount']").val(0);
               } else if(isNaN(groupAmount)) {
                   $.toast("组数请输入数字")
                   return false;
               }
               
               if(!movementNumber) {
                 $("input[name='movementNumber']").val(0);
               } else if(isNaN(movementNumber)) {
                 $.toast("每组次数请输入数字")
                 return false;
               }
               
               that.submitAdd(name, groupAmount, movementNumber, maxWeight, time, remark);*/
               that.submitAdd(name);
               $.closeModal(currentModal)
             }
           }
         ]
        })
      });
    },
    
    submitAdd: function(name) {
      var that = this;
      
      $.showIndicator();
      $.post(window.__CTX__.root+'/rounds/'+that.data.roundId+'/course-coursewares/'+that.data.courseScheduleId+'/coursewares/create-and-add', {"name": name})
      .done(function(response) {
        $.hideIndicator()
        utils.processResult(response, function(result) {
          window.location.href = window.__CTX__.root + '/course-schedules?day='+that.data.day+'&hour='+that.data.hour
        }, function(result) {
          $.toast(result.message || '提交失败')
        })
      })
    },
    /*submitAdd: function(name, groupAmount, movementNumber, maxWeight, time, remark) {
        var that = this;
        
        var url;
        var para;
        if(that.data.coursewareId) {
          url = window.__CTX__.root+'/rounds/'+that.data.roundId+'/course-coursewares/'+that.data.courseScheduleId+'/coursewares/'+that.data.coursewareId+'/edit-and-add';
          para = {"_method": "PATCH","coursewareId": that.data.coursewareId,"name": name, "groupAmount": !groupAmount ? 0 : groupAmount, "movementNumber": !movementNumber ? 0 : movementNumber, "maxWeight": maxWeight, "time": time, "remark": remark};
        } else {
          url = window.__CTX__.root+'/rounds/'+that.data.roundId+'/course-coursewares/'+that.data.courseScheduleId+'/coursewares/create-and-add';
          para = {"name": name, "groupAmount": !groupAmount ? 0 : groupAmount, "movementNumber": !movementNumber ? 0 : movementNumber, "maxWeight": maxWeight, "time": time, "remark": remark};
        }
        $.showIndicator();
        $.post(url, para)
        .done(function(response) {
          $.hideIndicator()
          utils.processResult(response, function(result) {
            window.location.href = window.__CTX__.root + '/course-schedules?day='+that.data.day+'&hour='+that.data.hour
          }, function(result) {
            $.toast(result.message || '提交失败')
          })
        })
    },*/
    
    initAdd: function() {
      var that = this;
      
      $(document).on("change", "input[name='movementId']", function() {
        var num = $("input[name='movementId']:checked").length;
        
        if(num > 0) {
          $(".add").text("添加动作(" + num + ")");
          $(".add").parent().show();
        } else {
          $(".add").parent().hide();
        }
      });
      
      $(document).on("click", ".add", function() {
        $.showIndicator();
        $.post(window.__CTX__.root+'/rounds/'+that.data.roundId+'/course-coursewares/'+that.data.courseScheduleId+'/coursewares', $("form[name='add_form']").serializeArray()).done(function(response) {
            $.hideIndicator()
            utils.processResult(response, function(result) {
              window.location.href = window.__CTX__.root + '/course-schedules?day='+that.data.day+'&hour='+that.data.hour
            }, function(result) {
              $.toast(result.message || '提交失败')
            })
          })
        console.log($("form[name='add_form']").serializeArray());
      });
    }
  }
})